//
//  VideosTableViewCell.m
//  XIWenLeJian
//
//  Created by lanou3g on 15/11/12.
//  Copyright © 2015年 蓝足. All rights reserved.
//

#import "VideosTableViewCell.h"
#import "VideoModel.h"
#import "UIImageView+WebCache.h"
#import "UIView+Extension.h"
#import "Masonry.h"

#define leftEdge 20

@implementation VideosTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // 标题
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.frame = CGRectMake(leftEdge, 5, 300, 30);
        _titleLabel.numberOfLines = 0;
        _titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];

        [self.contentView addSubview:_titleLabel];


        // 收藏
        _collectBT = [[UIButton alloc]init];
        _collectBT.frame = CGRectMake(leftEdge + 315, 5, 23, 23);
        [_collectBT setImage:[UIImage imageNamed:@"iconfont-shoucang (1)"] forState:(UIControlStateNormal)];
        [_collectBT addTarget:self action:@selector(collect:) forControlEvents:(UIControlEventTouchUpInside)];
        [self.contentView addSubview:_collectBT];
    
    // 视频封面
        _cdn_img = [[UIImageView alloc]init];
        [self.contentView addSubview:_cdn_img];
        
        // 播放按钮
        _playBT = [[UIButton alloc]init];
        [_playBT addTarget:self action:@selector(play:) forControlEvents:(UIControlEventTouchUpInside)];
        [_playBT setBackgroundImage:[UIImage imageNamed:@"iconfont-bofang-1"] forState:(UIControlStateNormal)];
        [self.contentView addSubview:_playBT];
        
        // 视频时长
        _videotimeLabel = [[UILabel alloc]init];
        _videotimeLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];

        [self.contentView addSubview:_videotimeLabel];

        // 播放次数
        _playcountLabel = [[UILabel alloc]init];
        _playcountLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];

        [self.contentView addSubview:_playcountLabel];

    }
    return self;
}

// 给 model 赋值
- (void)setModel:(VideoModel *)model
{
    _model = model;
    
    // 收藏
    if (_model.isCollect) {
        [_collectBT setBackgroundImage:[UIImage imageNamed:@"iconfont-shoucang"] forState:(UIControlStateNormal)];
    }else{
        [_collectBT setBackgroundImage:[UIImage imageNamed:@"iconfont-shoucang (1)"] forState:(UIControlStateNormal)];
    }
    [_collectBT mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(- 5);
        make.top.mas_equalTo(8);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
    
    // 标题 , 发布时间
    // substringFromIndex表示从指定位置开始截取字符串到最后，所截取位置包含该指定位置。
    NSString *timeStr = [_model.created_at substringFromIndex:5];
    
    NSString *str = [NSString stringWithFormat:@"%@--%@", timeStr, model.text];
    _titleLabel.text = str;
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    [_titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(leftEdge);
        make.top.mas_equalTo(8);
        make.right.mas_equalTo(-35);
        make.height.mas_equalTo(30);
    }];
    
    
    // 图片
    [self.cdn_img setImageWithURL:[NSURL URLWithString:model.image_small]];
    [_cdn_img mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(leftEdge);
        make.top.mas_equalTo(40);
        make.right.mas_equalTo(- 20);
        make.bottom.mas_equalTo(- 30);
    }];
    
    // 视频时长
    int time = [model.videotime intValue];
    int timeMin = time / 60;
    int timeSec = time % 60;
    if (timeSec < 10) {
        _videotimeLabel.text = [NSString stringWithFormat:@"时长%d :0%d", timeMin, timeSec + 1];
    }else{
        _videotimeLabel.text = [NSString stringWithFormat:@"时长%d :%d", timeMin, timeSec + 1];
    }
    [_videotimeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(leftEdge);
        make.bottom.mas_equalTo(- 5);
        make.size.mas_equalTo(CGSizeMake(75, 20));
    }];
    
    // 播放次数
    _playcountLabel.text = [NSString stringWithFormat:@"播放次数:%ld", model.playcount];
    [_playcountLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(100);
        make.bottom.mas_equalTo(-5);
        make.size.mas_equalTo(CGSizeMake(100, 20));
    }];
    
    [_playBT mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(60, 60));
        make.center.equalTo(_cdn_img);
    }];
}

// 收藏按钮的监听
- (void)collect:(UIButton *)bt
{
//    _model.isCollect = !_model.isCollect;
//    // 去执行block 的东西
//    self.reloadDataBlock();
}

// 播放按钮的监听
- (void)play:(UIButton *)bt
{
    self.movieBlock(_model.videouri);
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
